package com.wykj.business.controller;

import com.wykj.SpringBootStartApplication;
import com.wykj.business.entity.ResultMap;
import com.wykj.business.model.Tuser;
import com.wykj.business.service.TuserService;
import com.wykj.framework.spark.WordCountJava;

import ch.qos.logback.classic.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by Administrator on 2017/4/29.
 */
@Controller
@RequestMapping("/user")
public class TuserController {
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(TuserController.class);
    @Autowired
    private TuserService tuserService;

    @RequestMapping(value="/test",method = { RequestMethod.GET, RequestMethod.POST })
    public String index1(Model model,Integer pageNum,Integer pageSize) {
        model.addAttribute("title", "你好，这是我的第一个 SpringBoot Web Peoject! ");
        logger.info("你好，logback in flume!");
        return "user/index";
    }

    @RequestMapping(value="/findByPage",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public ResultMap findByPage(Integer pageNum,Integer pageSize) {
        ResultMap resultMap = new ResultMap();
        Page page = tuserService.findByPage(pageNum,pageSize);
        if (page != null) {
            resultMap.setList(page.getContent());
            resultMap.setState(true);
            return resultMap;
        }
        return resultMap;
    }

    @RequestMapping(value="/saveOrUpdate",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public ResultMap saveOrUpdate(Tuser tuser) {
        ResultMap resultMap = new ResultMap();
        try {
            tuserService.savaOrUpdate(tuser);
            resultMap.setData(tuser);
            resultMap.setState(true);
        }catch(Exception e){
            e.printStackTrace();
            resultMap.setState(false);
        }
        return resultMap;
    }
}
